package com.github.kezhenxu94.data.processor;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Objects;

/**
 * Created by kezhenxu on 1/6/17.
 *
 * @author kezhenxu (kezhenxu94@163.com)
 */
public class ProcessorChain<T> implements Processor<T> {
    protected Collection<Processor<T>> processors;

    public ProcessorChain() {
        this.processors = new LinkedList<>();
    }

    @Override
    public T transform(T input) throws Exception {
        for (Processor<T> processor : processors)
            input = processor.transform(input);
        return input;
    }

    public ProcessorChain<T> add(final Processor<T> processor) {
        Objects.requireNonNull(processor);
        this.processors.add(processor);
        return this;
    }

    public ProcessorChain<T> remove(final Processor<T> processor) {
        Objects.requireNonNull(processor);
        this.processors.remove(processor);
        return this;
    }

}
